package cemp.api;

import cemp.common.api.reponse.RestCommonResponse;
import cemp.common.api.reponse.RestStockAllResponse;
import cemp.common.api.reponse.RestStockDetailResponse;
import cemp.common.api.reponse.RestStockPlates;
import cemp.common.dto.response.PlateStocksDto;
import cemp.entity.StockAll;
import cemp.entity.StockPlate;
import cemp.mapper.StockAllMapper;
import cemp.mapper.StockPlateMapper;
import cemp.mapper.StockPlateStocksMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fetchdata.api.FetchDataApi;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
public class FetchDataApiImpl implements FetchDataApi {

    private final StockAllMapper stockAllMapper;
    private final StockPlateMapper stockPlateMapper;
    private final StockPlateStocksMapper stockPlateStocksMapper;

    @Override
    public List<RestStockDetailResponse> getStockDetails() {

        return null;
    }

    @Override
    public List<RestStockAllResponse> getAllStocks() {
        Page<StockAll> p = new Page<>(1,50);
        List<StockAll> result = stockAllMapper.selectPage(p,null).getRecords();
//        List<StockAll> result = stockAllMapper.selectList(null);
        return result.stream().map((Function<StockAll, RestStockAllResponse>) stockAll -> {
            RestStockAllResponse response = new RestStockAllResponse();
            BeanUtils.copyProperties(stockAll,response);
            return response;
        }).collect(Collectors.toList());
    }

    @Override
    public RestCommonResponse getPlates(Integer pageNum, Integer pageSize) {
        Page<StockPlate> p = new Page<>(pageNum,pageSize);
        Page<StockPlate> pResult = stockPlateMapper.selectPage(p,null);
        RestCommonResponse response = new RestCommonResponse();
        response.setData(pResult.getRecords().stream().map(stockPlate -> {
            RestStockPlates plate = new RestStockPlates();
            BeanUtils.copyProperties(stockPlate,plate);
            return plate;
        }).collect(Collectors.toList()));
        response.setTotal(pResult.getTotal());
        return response;
    }

    @Override
    public RestCommonResponse getPlateStocks(Integer pageNum, Integer pageSize,String platecode) {
        Page<PlateStocksDto> page = Page.of(pageNum, pageSize);
        Page<PlateStocksDto> result =  stockPlateStocksMapper.getPlateStocks(page,platecode);
        RestCommonResponse response = new RestCommonResponse();

        response.setData(result.getRecords());
        response.setTotal(result.getTotal());
        return response;
    }

    @Override
    public RestCommonResponse getStockInfo(String stockCode) {
        LambdaQueryWrapper<StockAll> lqw = new LambdaQueryWrapper<>();
        lqw.eq(StockAll::getStockCode,stockCode);
        RestCommonResponse response = new RestCommonResponse();
        response.setData(stockAllMapper.selectList(lqw));
        return response;
    }
}
